package com.accp.reporter.controller;
import com.accp.reporter.pojo.User;
import com.accp.reporter.service.UserService;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/user")
//这里用了@SessionAttributes，可以直接把model中的user(也就key)放入其中
//这样保证了session中存在user这个对象
@SessionAttributes("user")
public class UserController {


    @Autowired
    private UserService userService;


     //登陆
    @ResponseBody
    @RequestMapping(value ="/userLogin")
    public Object userLogin(User user, Model model){
        //调用service方法
        user = userService.userLogin(user.getUserName(),user.getPassword());
            Gson gson=new Gson();
            return gson.toJson(user);

    }
    //注销方法
    @RequestMapping("/outLogin")
    public String outLogin(HttpSession session){

        session.invalidate();
        return "index.jsp";
    }



    @ResponseBody
    @RequestMapping(value="/add")
    public String addUser(HttpServletRequest req, Model model) {
        System.out.println("come into UserController's add method....................");
        String userName= req.getParameter("userName");
        String realName = req.getParameter("realName");
        String password = req.getParameter("password");
        String tel = req.getParameter("tel");
        String email = req.getParameter("email");
        String idCard = req.getParameter("idCard");
        String age = req.getParameter("age");

        int user = userService.addUser(new User(userName,realName,password,tel,email,idCard,Integer.parseInt(age)));
            Gson gson=new Gson();
            return gson.toJson(user);
    }

    @ResponseBody
    @RequestMapping(value="/findUsername")
    public String findUsername(HttpServletRequest req,Model model) {
        String userName= req.getParameter("userName");
        User user=userService.finduserName(userName);
        Gson gson=new Gson();
        return gson.toJson(user);
    }






}
